package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.TravelCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentMongoRepository repository;

    @Override
    public TravelComment commentAdd(TravelComment comment) {
        //填充评论时间
        comment.setCreateTime(new Date());
        //区别普通评论和回复评论
        if(comment.getType() == TravelComment.TRAVEL_COMMENT_TYPE){//普通评论
            //根据id查询到原评论
            TravelComment refcomment = repository.findById(comment.getRefComment().getId()).get();
            //设置当前评论的引用
            comment.setRefComment(refcomment);
        }
        repository.save(comment);
        return comment;
    }

    @Override
    public List<TravelComment> findByUserId(Long userId) {
        return repository.findByUserId(userId);
    }

    @Override
    public List<TravelComment> listCommentByDetailId(Long travelId) {
        return repository.findByTravelId(travelId);
    }
}
